package com.ruoyi.ma.controller;

import java.io.*;
import java.net.URL;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
import java.util.concurrent.TimeUnit;

import cn.hutool.core.net.url.UrlPath;
import cn.hutool.core.util.IdUtil;
import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.config.Configure;
import com.deepoove.poi.plugin.table.LoopRowTableRenderPolicy;
import com.ruoyi.common.utils.poi.XWPFDocumentUtil;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.ma.domain.vo.MaMaterialManifestImportVo;
import lombok.RequiredArgsConstructor;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaCheckLogin;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.io.UrlResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.ma.domain.vo.MaReportFormPaymentVo;
import com.ruoyi.ma.domain.bo.MaReportFormPaymentBo;
import com.ruoyi.ma.service.IMaReportFormPaymentService;
import com.ruoyi.common.core.page.TableDataInfo;
import org.thymeleaf.context.Context;
import org.thymeleaf.spring5.SpringTemplateEngine;

/**
 * 往期材料支付报表
 *
 * @author tll
 * @date 2024-11-27
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/ma/reportFormPayment")
public class MaReportFormPaymentController extends BaseController {

    private final IMaReportFormPaymentService iMaReportFormPaymentService;

    private final ServletContext servletContext;
    private final SpringTemplateEngine springTemplateEngine;

    /**
     * 查询往期材料支付报表列表
     */
    @SaCheckLogin
    @GetMapping("/list")
    public TableDataInfo<MaReportFormPaymentVo> list(MaReportFormPaymentBo bo, PageQuery pageQuery) {
        return iMaReportFormPaymentService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出往期材料支付报表列表
     */
    @SaCheckLogin
    @Log(title = "往期材料支付报表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(MaReportFormPaymentBo bo, HttpServletResponse response) {
        List<MaReportFormPaymentVo> list = iMaReportFormPaymentService.queryList(bo);
        ExcelUtil.exportExcel(list, "往期材料支付报表", MaReportFormPaymentVo.class, response);
    }
    /**
     * 获取详情文件流对象
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response, MaReportFormPaymentBo bo) {
        if (1 == bo.getType()) { // 封面
            iMaReportFormPaymentService.importFm(response, bo);
        }
        if (2 == bo.getType()) { // 支付申请表
            iMaReportFormPaymentService.importZfsq(response, bo);
        }
        if (3 == bo.getType()) { // 支付明细表
            iMaReportFormPaymentService.importZfmx(response, bo);
        }
        if (4 == bo.getType()) { // 月度供货计算表
            iMaReportFormPaymentService.importYdgh(response, bo);
        }
    }

    /**
     * 获取往期材料支付报表详细信息
     *
     * @param reportId 主键
     */
    @SaCheckLogin
    @GetMapping("/{reportId}")
    public R<MaReportFormPaymentVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long reportId) {
        return R.ok(iMaReportFormPaymentService.queryById(reportId));
    }

    /**
     * 获取往期材料支付报表详细信息
     *
     */
    @SaCheckLogin
    @GetMapping("/detail")
    public R<MaReportFormPaymentVo> detail(MaReportFormPaymentBo bo) {
        return R.ok(iMaReportFormPaymentService.detail(bo));
    }

    /**
     * 新增往期材料支付报表
     */
    @SaCheckLogin
    @Log(title = "往期材料支付报表", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody MaReportFormPaymentBo bo) {
        return toAjax(iMaReportFormPaymentService.insertByBo(bo));
    }

    /**
     * 修改往期材料支付报表
     */
    @SaCheckLogin
    @Log(title = "往期材料支付报表", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody MaReportFormPaymentBo bo) {
        return toAjax(iMaReportFormPaymentService.updateByBo(bo));
    }

    /**
     * 删除往期材料支付报表
     *
     * @param reportIds 主键串
     */
    @SaCheckLogin
    @Log(title = "往期材料支付报表", businessType = BusinessType.DELETE)
    @DeleteMapping("/{reportIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] reportIds) {
        return toAjax(iMaReportFormPaymentService.deleteWithValidByIds(Arrays.asList(reportIds), true));
    }


    /**
     * 支付情况统计明细
     */
    @SaCheckLogin
    @GetMapping("/statistics")
    public R<Map<String, Object>> statistics(MaReportFormPaymentBo bo) {
        return R.ok(iMaReportFormPaymentService.statistics(bo));
    }
}
